package com.example.tossednfound;

import java.util.ArrayList;
import java.util.Collections;

import android.app.Dialog;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class ItemListViewActivity extends ListActivity {

	public static ArrayList<Item> list;
	ArrayAdapter<Item> adapter;
	public static boolean isSearched;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if(list == null || !isSearched)
			list = LoginActivity.ItemList;
		adapter = new ArrayAdapter<Item>(this,
				R.layout.activity_all_items, R.id.list_textView,
				list);
		setListAdapter(adapter);
		isSearched = false;
		ListView listView = getListView();
		listView.setTextFilterEnabled(true);

		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// When clicked, show a toast with the TextView text
				/*Toast.makeText(getApplicationContext(),
						LoginActivity.ItemList.get(position).toString(),
						Toast.LENGTH_SHORT).show();*/
				LoginActivity.currentItem = list
						.get(position);
				
				Intent itemProfileActivity = new Intent(
						"android.intent.action.ITEMPROFILEACTIVITY");
				startActivity(itemProfileActivity);

			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		getMenuInflater().inflate(R.menu.activity_all_items, menu);
		return true;
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onBackPressed()
	 */
	//@Override
	public void onBackPressed() {
		super.onBackPressed();
		if(LoginActivity.loggedIn instanceof RegUser) {
			Intent regHomeActivity = new Intent(
					"android.intent.action.REGHOMEACTIVITY");
			startActivity(regHomeActivity);
		}
		else if(LoginActivity.loggedIn instanceof AdminUser) {
			Intent adminHomeActivity = new Intent(
					"android.intent.action.ADMINHOMEACTIVITY");
			startActivity(adminHomeActivity);
		}
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int selected = item.getItemId();
		Dialog d = new Dialog(this);
		TextView tv = new TextView(this);
		
		d.setContentView(tv);
		if (selected == R.id.sort_category) {
			Collections.sort(list, new Item.CompareByCategory());
			adapter = new ArrayAdapter<Item>(this,
					R.layout.activity_all_items, R.id.list_textView,
					list);
			setListAdapter(adapter);

			ListView listView = getListView();
			listView.setTextFilterEnabled(true);
			d.setTitle("Items Successfully Sorted");
			tv.setText("Sorted by Category");
		} else if (selected == R.id.sort_date) {
			Collections.sort(list, new Item.CompareByDate());
			adapter = new ArrayAdapter<Item>(this,
					R.layout.activity_all_items, R.id.list_textView,
					list);
			setListAdapter(adapter);

			ListView listView = getListView();
			listView.setTextFilterEnabled(true);
			d.setTitle("Items Successfully Sorted");
			tv.setText("Sorted by Date");
		} else if (selected == R.id.sort_status) {
			Collections.sort(list, new Item.CompareByStatus());
			adapter = new ArrayAdapter<Item>(this,
					R.layout.activity_all_items, R.id.list_textView,
					list);
			setListAdapter(adapter);

			ListView listView = getListView();
			listView.setTextFilterEnabled(true);
			d.setTitle("Items Successfully Sorted");
			tv.setText("Sorted by Status");
		} else if (selected == R.id.search) {
			Intent SearchActivity = new Intent("android.intent.action.SEARCHACTIVITY");
			startActivity(SearchActivity);
		}
		
		adapter.notifyDataSetChanged();		
		d.setContentView(tv);
		d.show();
		return super.onOptionsItemSelected(item);
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onOptionsMenuClosed(android.view.Menu)
	 */
	@Override
	public void onOptionsMenuClosed(Menu menu) {
		adapter.notifyDataSetChanged();
		super.onOptionsMenuClosed(menu);
	}

}
